import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/list/section.dart';
import 'package:industry_net/pages/home/app_more/models/app_model.dart';
import 'cell.dart';
import '../main/models/home_menu.dart';
import '../../../base/common/img_util.dart';
import 'state.dart';

class AppManController extends GetxController {
  final state = AppManState();
  @override
  void onInit() {
    super.onInit();
    state.collectList.addAll(Get.arguments['collect']);
    // state.collectListObs.value = state.collectList;
    getUnAddData();
    setupData();
  }

  void getUnAddData() {
    state.unAddList = [
      HomeMenuModel()
        ..icon = 'menu_icon_Mall.png'.imgPathHome
        ..name = '待添加1',
      HomeMenuModel()
        ..icon = 'menu_icon_financial.png'.imgPathHome
        ..name = '待添加2',
      HomeMenuModel()
        ..icon = 'menu_icon_pear.png'.imgPathHome
        ..name = '待添加3',
    ];
  }

  void setupData() {
    List<SectionModel> _list = [];
    if (state.collectList.isNotEmpty) {
      var mod1 = AppModel()
        ..title = '首页应用'
        ..items = state.collectList;
      var sec1 = SectionModel(
          index: 0,
          itemBuilder: (context, index) {
            return AppManCell(
              mod: mod1,
              isAdd: false,
            );
          },
          sectionHeader: SizedBox(
            height: gap12,
          ),
          items: [mod1]);
      _list.add(sec1);
    }

    if (state.unAddList.isNotEmpty) {
      var mod2 = AppModel()
        ..title = '智能应用'
        ..items = state.unAddList;
      var sec2 = SectionModel(
          index: 0,
          itemBuilder: (context, index) {
            return AppManCell(
              mod: mod2,
              isAdd: true,
            );
          },
          items: [mod2]);
      _list.add(sec2);
    }

    state.dataSource.value = _list;
  }

  void actionItem(int index, HomeMenuModel mod, bool isAdd) {
    if (isAdd) {
      state.collectList.add(mod);
      state.unAddList.removeAt(index);
    } else {
      state.collectList.removeAt(index);
      state.unAddList.add(mod);
    }
    // state.collectListObs.value = state.collectList;
    // state.unAddListObs.value = state.unAddList;
    setupData();
  }

  void actionSave() {
    Get.back(result: state.collectList);
  }
}
